package com.next

import groovy.transform.CompileStatic
import groovy.transform.TypeChecked

class Activity
{
    Date startTime
    Date endTime

    Date actualStartTime
    Date actualEndTime

    String description

    ActivityType type
    ActivitySubtype subtype

    String status = "未执行"

    Double longitude = 0
    Double latitude = 0
    String city
    String address
    //地址
    String name

    Account account
    Opportunity opportunity
    Contact contact
    Campaign campaign
    Issue issue
    Asset asset

    User createBy
    User modifyBy
    User user
    User assignedTo

    Integer rating = 1

    Date createdDate = new Date()
    Date modifiedDate = new Date()

    Date completedDate

    static hasMany = [attachments: Attachment, notes: Note, users: ActivityTeam]

    static constraints = {
        status inList: ["未执行", "已推迟", "已完成", "已取消"]

        user nullable: true, blank: true

        description nullable: true, blank: true

        actualStartTime nullable: true, blank: true
        actualEndTime nullable: true, blank: true

        assignedTo nullable: true, blank: true

        longitude nullable: true, blank: true
        latitude nullable: true, blank: true
        city nullable: true, blank: true
        address nullable: true, blank: true

        // parent nullable: true, blank: true

        account nullable: true, blank: true, maxSize: 32
        campaign nullable: true, blank: true
        account nullable: true, blank: true
        contact nullable: true, blank: true
        issue nullable: true, blank: true
        asset nullable: true, blank: true
        opportunity nullable: true, blank: true

        createBy nullable: true, blank: true
        modifyBy nullable: true, blank: true
        createdDate nullable: true, blank: true
        modifiedDate nullable: true, blank: true

        completedDate nullable: true, blank: true

        rating nullable: true, blank: true, min: 1, max: 10
    }

    static mapping = {
        sort 'startTime', 'name'
    }

    @CompileStatic
    @TypeChecked
    String toString()
    {
        name
    }

    def springSecurityService

    def beforeInsert()
    {
        String username = springSecurityService?.getPrincipal()?.username
        User createBy = User.findByUsername(username)
        if (!user)
        {
            user = createBy
        }

        if (!assignedTo)
        {
            assignedTo = createBy
        }

        ActivityTeam activityTeam = new ActivityTeam(actvitity: this, user: user, teamRole: TeamRole.findByName('Edit'))
        this.addToUsers(activityTeam)

        if (assignedTo != user)
        {
            activityTeam = new ActivityTeam(actvitity: this, user: assignedTo, teamRole: TeamRole.findByName('Edit'))
            this.addToUsers(activityTeam)
        }

        if (user.shareActivityToSupervisor)
        {
            User reportTo = user.reportTo
            while (reportTo)
            {
                ActivityTeam activityTeam1 = new ActivityTeam(actvitity: this, user: reportTo, teamRole: TeamRole.findByName('Read'))
                this.addToUsers(activityTeam1)

                reportTo = reportTo.reportTo
            }
        }
    }

    def afterInsert()
    {
        def flexFieldCategories = FlexFieldCategory.findAllByObjectType('Activity')
        flexFieldCategories?.each {
            if (it?.condition)
            {
                def result = false
                try
                {
                    def shell = new GroovyShell()
                    def closure = shell.evaluate(it?.condition?.script)
                    result = closure(this)
                }
                catch (Exception e)
                {
                    log.error e
                }
                if (result)
                {
                    initFlexField(it)
                }
            }
            else
            {
                initFlexField(it)
            }
        }
    }

    def initFlexField(FlexFieldCategory flexFieldCategory)
    {
        def fields = FlexField.findAllByCategory(flexFieldCategory)
        fields?.each {
            def field = new FlexFieldInstance(category: flexFieldCategory, field: it, activity: this)
            field.save()
        }
    }

    def beforeUpdate()
    {
        modifiedDate = new Date()
        def username = springSecurityService?.getPrincipal()?.username
        modifyBy = User.findByUsername(username)
    }
}
